package com.quantdo.apex.elasticjob.model;

import java.io.Serializable;

/**
 * @author chenkuan
 * @version v1.0
 * @desc 通用响应封装
 * @date 2019/2/28 028 14:57
 */
public final class Response<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private Integer code;
	private String msg;
	private T result;

	private static final Integer SUCCESS = 0;

	public Response() {
		this(SUCCESS, "OK");
	}

	public Response(Integer code, String msg) {
		this.code(code).msg(msg);
	}

	public Response(Integer code, T result) {
		this.code = code;
		this.result = result;
	}

	public Response(Integer code, String msg, T result) {
		this.code = code;
		this.msg = msg;
		this.result = result;
	}

	public Response<T> code(Integer code) {
		this.setCode(code);
		return this;
	}

	public Response<T> msg(String msg) {
		this.setMsg(msg);
		return this;
	}

	public Response<T> result(T result) {
		this.setResult(result);
		return this;
	}

	public static <T> Response<T> ok() {
		return new Response<>();
	}

	public static <T> Response<T> fail(String msg) {
		return new Response<>(-1, msg);
	}

	public boolean isSuccess() {
		return SUCCESS.equals(this.code);
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}
}
